/**
 * Created by wengca on 2017/10/23 0023.
 */
var rp = require('request-promise');

class Request {
  constructor() {
    
  }
  /**
   * @param url
   * @param obj
   * @returns {Promise.<*>}
   * @constructor
   */
  async RequestPost(url,obj) {
    try {
     let options = {
        method: 'POST',
        uri: url,
       timeout: 3000,
        body: obj,
         headers: {
           "content-type": "application/json",
         },
        json: true // Automatically stringifies the body to JSON
      };
      let result = await  rp.post(options);
      return result;
    }
    catch (err) {
      console.error(err);
      return {code:500}
    }
  }
  
  /**
   * get获取到服务器信息
   * @param url
   * @param obj
   * @returns {Promise.<*>}
   * @constructor
   */
  async RequestGet(url) {
    try {
      console.log("RequestGet is",url);
      let result =await rp.get(url);
      return result;
    }
    catch (err) {
      console.log("RequestGet err",url, err);
      return {code:500}
    }
  }
}

module.exports = new Request();